package jmine.tec.web.wicket.pages;

import images.ImageResources;
import javascript.JSResources;

import org.apache.wicket.PageParameters;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.Session;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.JavascriptPackageResource;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.ResourceLink;
import org.apache.wicket.spring.injection.annot.SpringBean;

import bancosys.tec.persist.dao.BaseDAOFactory;
import bancosys.tec.persist.dao.BeanNotFoundException;
import bancosys.tec.security.SecurityService;
import bancosys.tec.security.impl.dao.SecurityClientDAO;
import bancosys.tec.security.impl.domain.SecurityClient;
import css.CSSResources;

/**
 * Auxiliar na construção de Template/MonitorTemplate
 * 
 * @author nara.ribeiro
 * @created Jan 27, 2012
 */
public abstract class AbstractTemplate extends WebPage {

    @SpringBean(name = "daoFactory")
    private BaseDAOFactory baseDAOFactory;

    @SpringBean
    private SecurityService securityService;

    /**
     * Construtor
     */
    public AbstractTemplate() {
        super();
    }

    /**
     * Construtor
     * 
     * @param parameters {@link PageParameters}
     */
    public AbstractTemplate(PageParameters parameters) {
        super(parameters);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onInitialize() {
        super.onInitialize();
        this.initPage();
    }

    /**
     * Inicializa a página principal
     */
    private void initPage() {
        this.add(this.getTitlePage());
        this.add(new ResourceLink("favicon", new ResourceReference(ImageResources.class, "favicon.png")));

        this.addCSS("search_page.css");
        this.addCSS("form_page.css");
        this.addCSS("form_fields.css");
        this.addCSS("search_results.css");
        this.addCSS("tabs.css");
        this.addCSS("report_results.css");
        this.addCSS("report_page.css");
        this.addCSS("antigo.css"); // XXX deve ser apagado em breve...

    }

    /**
     * @return Label com o título da página.
     */
    private Label getTitlePage() {
        SecurityClientDAO securityClientDAO = this.baseDAOFactory.getDAOByClass(SecurityClientDAO.class);
        try {
            SecurityClient securityClient = securityClientDAO.findByPk(this.securityService.getSystemId());
            return new Label("titlePage", securityClient.getName());
        } catch (BeanNotFoundException e) {
            return new Label("titlePage", "Sistema");
        }
    }

    /**
     * Adiciona um arquivo CSS a página.
     * 
     * @param cssFilename o nome do arquivo CSS.
     */
    protected final void addCSS(String cssFilename) {
        this.add(CSSPackageResource.getHeaderContribution(new ResourceReference(CSSResources.class, cssFilename, Session.get().getLocale(),
                Session.get().getStyle())));
    }

    /**
     * Adiciona um arquivo JavaScript a página.
     * 
     * @param jsFilename o nome do arquivo JavaScript.
     */
    protected final void addJS(String jsFilename) {
        this.add(JavascriptPackageResource.getHeaderContribution(new ResourceReference(JSResources.class, jsFilename, Session.get()
                .getLocale(), Session.get().getStyle())));
    }

    /**
     * @return the securityService
     */
    public SecurityService getSecurityService() {
        return securityService;
    }

}
